#include "AssetManager.h"




AssetManager::AssetManager(void)
{
}


AssetManager::~AssetManager(void)
{
}


MeshDesc* AssetManager::LoadMeshFromFile(std::string filename)
{
  return DirectX::MeshLoader::GetInstance().LoadMeshFromFile(filename);  
}

DirectX::IRenderable* AssetManager::Load(std::string filename)
{
  MeshDesc* data = LoadMeshFromFile(filename);
  if(data != 0)
    return new DirectX::Mesh(data);
  return 0;
}

DirectX::IRenderable* AssetManager::Load(Chess::FigureTypeEnum figure, Chess::ColorEnum color)
{
  std::string textureName = color == Chess::ColorEnum::WHITE ? "white.dds" : "black.dds";
  DirectX::TextureManager::GetInstance().LoadTexture(textureName);

  MeshDesc* data = 0;
  std::string model = "";


  switch(figure)
  {
  case Chess::FigureTypeEnum::FIELD:
    return new Game::FieldMesh(new DirectX::Mesh(LoadMeshFromFile("marker_selected.obj")), new DirectX::Mesh(LoadMeshFromFile("marker_reachable.obj")));
    break;
  case Chess::FigureTypeEnum::BOARD:
    model = "chessboard.obj";
    break;
  case Chess::FigureTypeEnum::BISHOP:
    model = "bishop.obj";
    break;
  case Chess::FigureTypeEnum::KING:
    model = "king.obj";
    break;
  case Chess::FigureTypeEnum::KNIGHT:
    model = "knight.obj";
    break;
  case Chess::FigureTypeEnum::PAWN:
    model = "pawn.obj";
    break;
  case Chess::FigureTypeEnum::QUEEN:
    model = "queen.obj";
    break;
  case Chess::FigureTypeEnum::ROOK:
    model = "rook.obj";
    break;    
  }

  data = LoadMeshFromFile(model);
  if(data == 0)
    return 0;

  if(figure == Chess::FigureTypeEnum::BOARD)
    return new Game::BoardMesh(data);

  Material mat = data->Materials[0];
  data->Materials = new Material[2];
  data->NumMaterials = 2;
  data->Materials[0] = mat;  
  DirectX::Obj::InitMaterial(&data->Materials[1]);
  data->Materials[1].TextureName = textureName;

  switch(figure)
  {
  case Chess::FigureTypeEnum::BISHOP:
    return new Game::BishopMesh(data, color);
  case Chess::FigureTypeEnum::KING:
    return new Game::KingMesh(data, color);
  case Chess::FigureTypeEnum::KNIGHT:
    return new Game::KnightMesh(data, color);
  case Chess::FigureTypeEnum::PAWN:
    return new Game::PawnMesh(data, color);
  case Chess::FigureTypeEnum::QUEEN:
    return new Game::QueenMesh(data, color);
  case Chess::FigureTypeEnum::ROOK:
    return new Game::RookMesh(data, color);
  }
}
